/**
 * 
 */
package jvs.core.http.handler;


import java.util.logging.Logger;

import jvs.core.http.Request;
import jvs.core.http.Response;


/**
 * @author qiangli
 */
public class Handler implements jvs.core.http.Handler {

	protected Logger log = Logger.getLogger(getClass().getName());


	protected void doGet(Request req, Response res) throws Exception {
	}

	protected void doPost(Request req, Response res) throws Exception {
	}

	
	public boolean handle(Request req, Response res) {
		log("handle request: " + req);

		try {
			service(req, res);
		} catch (Exception e) {
			res.error(e);
		}
		return true;
	}

	protected void log(String s) {
		System.out.println(s);
		log.fine(s);
	}
	
	protected void service(Request req, Response res) throws Exception {
		if (req.method.equals("GET")) {
			doGet(req, res);
		} else if (req.method.equals("POST")) {
			doPost(req, res);
		} else if (req.method.equals("HEAD")) {
			doHead(req, res);
		} else {
			res.ni(req.toString());
		}
	}

	protected String nvl(String s) {
		return s == null ? "" : s;
	}

	protected void doHead(Request req, Response res) {
	}
}
